#include<list>
#include <iostream>
using namespace std;
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param n int整型
     * @param m int整型
     * @return int整型
     */
    int LastRemaining_Solution(int n, int m) {
        // write code here
        list<int> l(n, 0);
        int i = 0;
        for (auto& e : l)
        {
            e = i++;
        }

        auto it = l.begin();
        while (l.size() != 1)
        {
            int k = m;
            while (--k)
            {
                it++;
                if (it == l.end())
                    it++;
            }
            it = l.erase(it);
        }

        return *it;
    }
};

int main()
{
    cout << Solution().LastRemaining_Solution(5, 3) << endl;
    return 0;
}